package com.gitee.huanminabc.utils_tools.config_refresh.listener;

import com.gitee.huanminabc.utils_tools.config_refresh.core.ConfigRefreshProcessor;
import com.gitee.huanminabc.utils_tools.config_refresh.event.ConfigChangeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@Order(1)
public class ConfigValueChangeListener implements ApplicationListener<ConfigChangeEvent> {
    @Autowired
    private ConfigRefreshProcessor configRefreshProcessor;
    @Override
    public void onApplicationEvent(ConfigChangeEvent event) {
        //查找符合条件的bean
        List<Object> beans = configRefreshProcessor.getBeans(event.getKey());
        for (Object bean : beans) {
            //获取bean的属性
            List<String> fieldValues = configRefreshProcessor.getFieldValues(bean);
            //刷新属性
            configRefreshProcessor.refreshField(bean, fieldValues, event.getKey(), event.getValue().toString());
        }
    }
}
